﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OrionBank.Forms
{
    public partial class EcheanciersGest : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        private Int32 idC;
        public EcheanciersGest(Int32 idCompte)
        {
            InitializeComponent();
            idC = idCompte;

            ChargeGrille();
        }

        private void ChargeGrille()
        {
            try
            {
                DataSet ds = Classe.Echeancier.ChargeGrilleEcheance(idC);
                dgvEcheance.DataSource = ds;
                dgvEcheance.DataMember = "Echeancier";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            try
            {
                Forms.Echeancier ea = new Echeancier(idC, "INSERT");
                ea.ShowDialog();

                ChargeGrille();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void kryptonButton3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Etes-vous sur de supprimer cette échéance ?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    Classe.Echeancier.Delete(Int32.Parse(dgvEcheance.SelectedRows[0].Cells["Ident. Echeance"].Value.ToString()));
                    ChargeGrille();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void kryptonButton2_Click(object sender, EventArgs e)
        {
            try
            {
                Forms.Echeancier em = new Echeancier(Int32.Parse(dgvEcheance.SelectedRows[0].Cells["Ident. Echeance"].Value.ToString()), "UPDATE");
                em.ShowDialog();

                ChargeGrille();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void kryptonButton4_Click(object sender, EventArgs e)
        {
            try
            {
                Int32 i = Classe.Echeancier.InsereEcheance(txtDateInsereEch.Value, idC);
                MessageBox.Show(i + " Opérations insérées.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ChargeGrille();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dgvEcheance_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                Forms.Echeancier em = new Echeancier(Int32.Parse(dgvEcheance.SelectedRows[0].Cells["Ident. Echeance"].Value.ToString()), "UPDATE");
                em.ShowDialog();

                ChargeGrille();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
